package com.niodata.dp.core.security;

import com.niodata.dp.core.service.ServiceInfoMapper;
import com.niodata.dp.core.service.SystemService;
import com.niodata.dp.core.service.SystemServices;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("ServiceSecurityManagerImpl")
public class ServiceSecurityManagerImpl implements ServiceSecurityManager {

  @Autowired
  private ServiceInfoMapper serviceInfoMapper;
  @Autowired
  private ServiceAuthMapper serviceAuthMapper;

  @Override
  @Transactional
  public void grantUserServicePermission(long userId, SystemService service) {
    ServiceAuth auth = new ServiceAuth();
    auth.setTargetId(userId);
    auth.setTargetType("USER");
    auth.setAuthTime(new Date());
    auth.setService(service.getName());
    serviceAuthMapper.addServiceAuth(auth);
  }

  @Override
  @Transactional
  public void grantGroupServicePermission(long groupId, SystemService service) {
    ServiceAuth auth = new ServiceAuth();
    auth.setTargetId(groupId);
    auth.setTargetType("GROUP");
    auth.setAuthTime(new Date());
    auth.setService(service.getName());
    serviceAuthMapper.addServiceAuth(auth);
  }

  @Override
  public void revokeServicePermission(ServiceAuth serviceAuth) {
    serviceAuthMapper.deleteAuth(serviceAuth.getService(),
          serviceAuth.getTargetId(), serviceAuth.getTargetType());
  }

  @Override
  public void revokeGroupServicePermission(long groupId, SystemService service) {
    serviceAuthMapper.deleteAuth(service.getName(),
          groupId, "GROUP");
  }

  @Override
  public void revokeUserServicePermission(long userId, SystemService service) {
    serviceAuthMapper.deleteAuth(service.getName(),
          userId, "USER");
  }

  @Override
  public List<SystemService> getUserGrantedServices(long userId) {
    List<ServiceAuth> auths = serviceAuthMapper.getServiceAuthList(userId, "USER");
    List<SystemService> serviceList = new ArrayList<>(auths.size());
    for (ServiceAuth auth : auths) {
      SystemService service = SystemServices.getSystemService(auth.getService());
      if (service != null) {
        serviceList.add(service);
      }
    }
    return serviceList;
  }

  @Override
  public boolean isGroupGrantedService(long groupId, SystemService service) {
    List<SystemService> serviceList = this.getGroupGrantedServices(groupId);
    for (SystemService srv : serviceList) {
      if (srv.getName().equals(service.getName())) {
        return true;
      }
    }
    return false;
  }

  @Override
  public List<SystemService> getGroupGrantedServices(long groupId) {
    List<ServiceAuth> auths = serviceAuthMapper.getServiceAuthList(groupId, "GROUP");
    List<SystemService> serviceList = new ArrayList<>(auths.size());
    for (ServiceAuth auth : auths) {
      SystemService service = SystemServices.getSystemService(auth.getService());
      if (service != null) {
        serviceList.add(service);
      }
    }
    return serviceList;
  }

  @Override
  public boolean isUserGrantedService(long userId, SystemService service) {
    List<SystemService> serviceList = this.getUserGrantedServices(userId);
    for (SystemService srv : serviceList) {
      if (srv.getName().equals(service.getName())) {
        return true;
      }
    }
    return false;
  }
}
